// Copyright 2024 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ctl

import (
	"github.com/matrixorigin/matrixone/pkg/objectio"
	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
	"github.com/stretchr/testify/require"
	"math"
	"testing"
)

func TestParseArgs(t *testing.T) {
	cases := []struct {
		s string
		r arguments
		e bool
	}{
		{
			s: "t:db1.table1",
			r: arguments{mergeType: tableMergeType, db: "db1", tbl: "table1", accountId: math.MaxUint64, targetObjSize: defaultTargetObjectSize},
		},
		{
			s: "t:db1.table1:small",
			r: arguments{mergeType: tableMergeType, db: "db1", tbl: "table1", accountId: math.MaxUint64, filter: "small", targetObjSize: defaultTargetObjectSize},
		},
		{
			s: "t:db1.table1:small:1M",
			r: arguments{mergeType: tableMergeType, db: "db1", tbl: "table1", accountId: math.MaxUint64, filter: "small", targetObjSize: common.Const1MBytes},
		},
		{
			s: "t:db1",
			e: true,
		},
		{
			s: "t:db1.table1.1:small:1M",
			r: arguments{mergeType: tableMergeType, db: "db1", tbl: "table1", accountId: 1, filter: "small", targetObjSize: common.Const1MBytes},
		},
		{
			s: "t:10000",
			e: true,
		},
		{
			s: "t:10000.1",
			r: arguments{mergeType: tableMergeType, db: "10000", tbl: "1", accountId: math.MaxUint64, targetObjSize: defaultTargetObjectSize},
		},
		{
			s: "o:10000:018f27b6-c6e1-7bef-a1e8-0f639ddedeef_0,018f27b6-c6e1-7bef-a1e8-0f639ddede00_0",
			r: arguments{db: "", tbl: "10000", accountId: math.MaxUint64, objs: []objectio.ObjectStats{
				{
					0x01, 0x8f, 0x27, 0xb6, 0xc6, 0xe1, 0x7b, 0xef,
					0xa1, 0xe8, 0x0f, 0x63, 0x9d, 0xde, 0xde, 0xef,
					0x00, 0x00, 0x30, 0x31, 0x38, 0x66, 0x32, 0x37,
					0x62, 0x36, 0x2d, 0x63, 0x36, 0x65, 0x31, 0x2d,
					0x37, 0x62, 0x65, 0x66, 0x2d, 0x61, 0x31, 0x65,
					0x38, 0x2d, 0x30, 0x66, 0x36, 0x33, 0x39, 0x64,
					0x64, 0x65, 0x64, 0x65, 0x65, 0x66, 0x5f, 0x30,
					0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				},
				{
					0x01, 0x8f, 0x27, 0xb6, 0xc6, 0xe1, 0x7b, 0xef,
					0xa1, 0xe8, 0x0f, 0x63, 0x9d, 0xde, 0xde, 0x00,
					0x00, 0x00, 0x30, 0x31, 0x38, 0x66, 0x32, 0x37,
					0x62, 0x36, 0x2d, 0x63, 0x36, 0x65, 0x31, 0x2d,
					0x37, 0x62, 0x65, 0x66, 0x2d, 0x61, 0x31, 0x65,
					0x38, 0x2d, 0x30, 0x66, 0x36, 0x33, 0x39, 0x64,
					0x64, 0x65, 0x64, 0x65, 0x30, 0x30, 0x5f, 0x30,
					0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				},
			}, targetObjSize: defaultTargetObjectSize},
		},
		{
			s: "o:10000:018f27b6-c6e1-7bef-a1e8-0f639ddedeef_0,018f27b6-c6e1-7bef-a1e8-0f639ddede00_0:1M",
			r: arguments{db: "", tbl: "10000", accountId: math.MaxUint64, objs: []objectio.ObjectStats{
				{
					0x01, 0x8f, 0x27, 0xb6, 0xc6, 0xe1, 0x7b, 0xef,
					0xa1, 0xe8, 0x0f, 0x63, 0x9d, 0xde, 0xde, 0xef,
					0x00, 0x00, 0x30, 0x31, 0x38, 0x66, 0x32, 0x37,
					0x62, 0x36, 0x2d, 0x63, 0x36, 0x65, 0x31, 0x2d,
					0x37, 0x62, 0x65, 0x66, 0x2d, 0x61, 0x31, 0x65,
					0x38, 0x2d, 0x30, 0x66, 0x36, 0x33, 0x39, 0x64,
					0x64, 0x65, 0x64, 0x65, 0x65, 0x66, 0x5f, 0x30,
					0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				},
				{
					0x01, 0x8f, 0x27, 0xb6, 0xc6, 0xe1, 0x7b, 0xef,
					0xa1, 0xe8, 0x0f, 0x63, 0x9d, 0xde, 0xde, 0x00,
					0x00, 0x00, 0x30, 0x31, 0x38, 0x66, 0x32, 0x37,
					0x62, 0x36, 0x2d, 0x63, 0x36, 0x65, 0x31, 0x2d,
					0x37, 0x62, 0x65, 0x66, 0x2d, 0x61, 0x31, 0x65,
					0x38, 0x2d, 0x30, 0x66, 0x36, 0x33, 0x39, 0x64,
					0x64, 0x65, 0x64, 0x65, 0x30, 0x30, 0x5f, 0x30,
					0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				},
			}, targetObjSize: common.Const1MBytes},
		},
		{
			s: "o:db1.table1:018f27b6-c6e1-7bef-a1e8-0f639ddedeef_0,018f27b6-c6e1-7bef-a1e8-0f639ddede00_0",
			e: true,
		},
	}

	for _, c := range cases {
		a, e := parseArgsForPartitionTables(c.s)
		if c.e {
			require.Error(t, e)
		} else {
			require.Equal(t, c.r, a)
		}
	}
}
